package com.dl.mian.http;

import com.dl.mian.http.gson.ArrayDefault0Adapter;
import com.dl.mian.http.gson.DoubleDefault0Adapter;
import com.dl.mian.http.gson.FloatDefault0Adapter;
import com.dl.mian.http.gson.IntegerDefault0Adapter;
import com.dl.mian.http.gson.LongDefault0Adapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

/**
 * Created by dl on 2020/7/12.
 */
public class GsonUtil {
    private static Gson gson;

    /**
     * 获得一个可重用的Gson对象
     *
     * @return
     */
    public static Gson gson() {
        if (gson == null) {
            gson = new GsonBuilder()
                    .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(Float.class, new FloatDefault0Adapter())
                    .registerTypeAdapter(float.class, new FloatDefault0Adapter())
                    .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                    .registerTypeAdapter(long.class, new LongDefault0Adapter())
                    .registerTypeAdapter(List.class, new ArrayDefault0Adapter())
                    .create();
        }
        return gson;
    }
}
